﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;
using Tool.Core;

namespace Tool
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        /**列表规则
      * Title 为列名称
      * ListName 为列表名称
      * FieldType为列类型
      * TemplateName 为list继承的末班
      * Description 为列描述
  *Required 是否为必填项目
      * */
        private void GenerateList()
        {
            SPWeb adminWeb = Common.GetAdminWeb();
            //设置配置表的名称
            SPList list = adminWeb.Lists["ListDefinition"];
            SPQuery query = new SPQuery();
            query.Query = @" <OrderBy>
                                <FieldRef Name='ListName' Ascending='True' />
                            </OrderBy>";


            SPListItemCollection items = list.GetItems(query);
            List<ModelListCreator> lists = new List<ModelListCreator>();
            string currentListName = string.Empty;
            ModelListCreator listCreator = null;
            for (int i = 0; i < items.Count; i++)
            {
                SPListItem item = items[i];
                string listName = item["ListName"].ToString();
                if (listName == currentListName)
                {
                    listCreator = lists[lists.Count - 1];
                }
                else
                {
                    listCreator = new ModelListCreator();
                    listCreator.Name = listName;
                    listCreator.TemplateName = item["TemplateName"].ToString();
                    currentListName = listName;
                    lists.Add(listCreator);
                }
                ModelFieldCreator fieldCreator = new ModelFieldCreator();
                fieldCreator.DisplayName = item["Title"].ToString();
                fieldCreator.SPFieldType = item["FieldType"].ToString();
                fieldCreator.Description = item["Description"] == null ? "" : item["Description"].ToString();
                fieldCreator.Required = item["Required"].ToString();
                listCreator.Fields.Add(fieldCreator);
            }

            ListOperation.CreateList(lists);

        }




        private void generateListToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GenerateList();
            MessageBox.Show("操作完成");
        }

        private void createFormToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DeleteListInfo d = new DeleteListInfo();
            d.Show();

        }

    }
}
